﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameStateManagement
{
    public class Background : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Matrix worldTransformMatrix;
        protected float fScale;
        protected Vector3 vWorldPosition;
        protected Quaternion qRotation;
        protected bool hasChanged;

        protected Model actorModel;
        protected ContentManager contentManager;
        protected Matrix[] actorBones;
        protected Utils.Timer timers;

        public BoundingSphere ModelBounds;
        public BoundingSphere WorldBounds;

        protected bool followCam;

        public Background(Game game)
            : base(game)
        {
            timers = new Utils.Timer();
            worldTransformMatrix = Matrix.Identity;

            fScale = 7.0f;
            vWorldPosition = new Vector3(0.0f, 0.0f, 0.0f);
            qRotation = Quaternion.Identity;
            hasChanged = false;

            followCam = true;
        }

        public override void Initialize()
        {

            base.Initialize();

        }

        public float Scale
        {
            get { return fScale; }
            set
            {
                fScale = value;
                hasChanged = true;
            }
        }

        public Vector3 Position
        {
            get { return vWorldPosition; }
            set
            {
                vWorldPosition = value;
                hasChanged = true;
            }
        }

        public Quaternion Rotation
        {
            get { return qRotation; }
            set { qRotation = value; }
        }

        public void switchCam()
        {
            followCam = !followCam;
        }

        public void updateWorldTransformMatrix()
        {
            Matrix scaleMatrix = Matrix.CreateScale(fScale);
            Matrix rotationMatrix = Matrix.CreateFromQuaternion(qRotation);
            Matrix translationMatrix = Matrix.CreateTranslation(vWorldPosition);

            worldTransformMatrix = scaleMatrix;
            worldTransformMatrix *= rotationMatrix;
            worldTransformMatrix *= translationMatrix;

            WorldBounds.Center = vWorldPosition;
            WorldBounds.Radius = ModelBounds.Radius * fScale;
        }

        public override void Update(GameTime gameTime)
        {


            timers.Update(gameTime);
            base.Update(gameTime);
        }

        protected override void LoadContent()
        {
            contentManager = new ContentManager(Game.Services, "Content");
            actorModel = contentManager.Load<Model>("StarBackground");
            actorBones = new Matrix[actorModel.Bones.Count];


            base.LoadContent();

            foreach (ModelMesh mesh in actorModel.Meshes)
            {
                ModelBounds = BoundingSphere.CreateMerged(ModelBounds,
                mesh.BoundingSphere);
            }

        }

        protected override void UnloadContent()
        {
            contentManager.Unload();
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (hasChanged)
            {
                updateWorldTransformMatrix();
                hasChanged = false;
            }

            GraphicsDevice.RenderState.DepthBufferEnable = true;
            actorModel.CopyAbsoluteBoneTransformsTo(actorBones);
            foreach (ModelMesh mesh in actorModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World =
                    actorBones[mesh.ParentBone.Index] * worldTransformMatrix;
                    if (followCam)
                    {
                        effect.View = GameplayScreen.camera.View;
                        effect.Projection = GameplayScreen.camera.Projection;
                    }
                    else
                    {
                        effect.View = GameplayScreen.CameraMatrix;
                        effect.Projection = GameplayScreen.ProjectionMatrix;
                    }
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.AmbientLightColor = GameplayScreen.ambientLightColor;
                    //effect.DirectionalLight0.DiffuseColor = GameplayScreen.diffuseColor;
                    //effect.DirectionalLight0.Direction = GameplayScreen.diffuseDirection;
                    //effect.SpecularColor = GameplayScreen.specularColor;
                    //effect.SpecularPower = GameplayScreen.specularPower;

                }
                mesh.Draw();
            }
            base.Draw(gameTime);
        }


    }
}
